﻿using System.ServiceModel.Activation;
using System.Threading;
using System.Web;
using Newtonsoft.Json;
using TTprotons.Crm.ExternalView.Service.Common;
using TTprotons.Crm.ExternalView.Service.Crm;
using TTprotons.Crm.ExternalView.Service.Properties;
using WcfRestContrib.ServiceModel.Description;

namespace TTprotons.Crm.ExternalView.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ViewService" in code, svc and config file together.
    [WebDispatchFormatterConfiguration("application/xml")]
    [WebDispatchFormatterMimeType(typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.PoxDataContract), "application/xml", "text/xml")]
    [WebDispatchFormatterMimeType(typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.DataContractJson), "application/json")]
    [WebDispatchFormatterMimeType(typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.FormUrlEncoded), "application/x-www-form-urlencoded")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ViewService : IViewService
    {
        public string GetViewData(string password, string viewName, string filter)
        {
            var extViewManager = new ExternalViewManager();
            if (!extViewManager.IsParametersValid())
            {
                // Service not configured yet.
                return extViewManager.ConstructErrorMessage(Resources.NOTCONFIGURED_MESSAGE).ToString();
            }

            // Validate password
            if (!extViewManager.CanFetchView(password, viewName))
            {
                Thread.Sleep(5000);
                return extViewManager.ConstructErrorMessage(Resources.INVALIDPASSWORD_MESSAGE).ToString();
            }

            var result = extViewManager.FetchView(viewName, filter);
            if (HttpContext.Current != null && HttpContext.Current.Request.Headers["Accept"].EqualsIgnoreCase("application/json"))
            {
                return result == null ? string.Empty : JsonConvert.SerializeXNode(result);
            }

            return result.Encode("ISO-8859-1");
        }

        public string GetPagedViewData(string password, string viewName, string filter, string pageNo)
        {
            int iPageNo;
            if (!int.TryParse(pageNo, out iPageNo))
                iPageNo = 1;

            var extViewManager = new ExternalViewManager();

            if (!extViewManager.IsParametersValid())
            {
                // Service not configured yet.
                return extViewManager.ConstructErrorMessage(Resources.NOTCONFIGURED_MESSAGE).ToString();
            }

            // Validate password
            if (!extViewManager.CanFetchView(password, viewName))
            {
                Thread.Sleep(5000);
                return extViewManager.ConstructErrorMessage(Resources.INVALIDPASSWORD_MESSAGE).ToString();
            }

            var result = extViewManager.FetchView(viewName, iPageNo, filter);

            if (HttpContext.Current != null && HttpContext.Current.Request.Headers["Accept"].EqualsIgnoreCase("application/json"))
            {
                return result == null ? string.Empty : JsonConvert.SerializeXNode(result);
            }
            
            return result.Encode("ISO-8859-1");
        }
    }
}